package com.aaa.service;

import com.aaa.dao.ClassDao;
import com.aaa.dao.impl.ClassDaoImpl;
import com.aaa.entity.Pager;
import com.aaa.util.PageUtil;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;
import java.util.Map;

public class ClassServiceImpl implements ClassService{
    ClassDao dao=new ClassDaoImpl();
    public List<Map> query_course() {
        return dao.query_course();
    }
//查询班级是够已经存在！！！！
    @Override
    public List<Map> selectClassRoom(String username) {
        return dao.selectClassRoom(username);
    }

    @Override
    public int addClassRoom(String class_name) {
        return dao.addClassRoom(class_name);
    }

    @Override
    public List<Map> queryOneClass(int classid) {
        return dao.queryOneClass(classid);
    }

    @Override
    public int updateOneClass(List list) {
        List<Map> map=dao.queryOneClass(Integer.valueOf(String.valueOf(list.get(8))));
        if(!map.get(0).get("classroom").equals(list.get(5))){
           int result=dao.updateClassRoomIsNotUse(String.valueOf(map.get(0).get("classroom")));
        }
        return dao.updateOneClass(list);
    }

    @Override
    public int delete_class(int classid) {
        List<Map> map=dao.queryOneClass(classid);
        int result=dao.updateClassRoomIsNotUse(String.valueOf(map.get(0).get("classroom")));
        return dao.delete_class(classid);
    }

    @Override
    public List<Map> query_class() {
        return dao.query_class();
    }

    @Override
    public Pager query_by_top(int curPage, int pageSize, Map condation) {
        count_acnumber();
        pageSize= PageUtil.PAGE_SIZE;
        return dao.query(curPage,pageSize,condation);
    }

    @Override
    public List<Map> query_class_room() {
        return dao.query_class_room();
    }

    @Override
    public Pager query_class_room_pagers(int curPage) {
        int pageSize= PageUtil.PAGE_SIZE;
        return dao.query_class_room( curPage, pageSize);
    }

    @Override
    public int add_class_up(List list) {
        dao.updateClassRoomIsUse(String.valueOf(list.get(5)));
        return dao.add_class_up(list);
    }

    @Override
    public String count_acnumber() {
        String success="none";
        int count=0;
        List<Map> map=dao.count_acnumber();
        for (int i = 0; i <map.size() ; i++) {
            int result=-1;
            int acnumber=Integer.valueOf(String.valueOf(map.get(i).get("acnumber")));
            int classid=Integer.valueOf(String.valueOf(map.get(i).get("classid")));
            result=dao.update_acnumber(classid,acnumber);
            if(result==1){
                count++;
            }
            if(count==map.size()){
                success="yes";
            }else{
                success="no";
            }
        }
        return success;
    }

    @Override
    public List<Map> query_without_class() {
        return dao.query_without_class();
    }

    @Override
    public List<Map> query_class_one(int id) {
        return dao.query_class_one(id);
    }

    @Override
    public int update_student_classid(String[] ids, int classid) {
        String success="none";
        int count=0;
        if(ids.length!=0){
            for (int i = 0; i <ids.length ; i++) {
                int result=-1;
                result=dao.update_student_classid(Integer.parseInt(ids[i]),classid);
                if(result!=-1){
                    count++;
                }
            }
            if(count==ids.length){
                success="yes";
                System.out.println(success);
            }else{
                success="no";
                System.out.println(success);
            }
        }
        return count;
    }

    @Override
    public int update_class_progressing(int classid) {
        int result=-1;
        List<Map> map=query_class_one(classid);
        if(Integer.valueOf(String.valueOf(map.get(0).get("progressing")))==1){
            result=dao.update_class_progressing(classid,0,1);
        }else{
            result=dao.update_class_progressing(classid,1,0);
        }
        return result;
    }

    @Override
    public int update_class_room(String className,String room_name) {
        List<Map>map=dao.selectClassRoom(className);
        if(map.size()>=1){
            return -2;
        }
        return dao.update_class_room(className,room_name);
    }

    @Override
    public int deleteClassRoom(String roomName) {
        return dao.deleteClassRoom(roomName);
    }
}
